package com.yhq.thrift.demo.springserver;

import lombok.extern.slf4j.Slf4j;
import org.apache.thrift.TException;
import org.springframework.stereotype.Service;
import shared.SharedStruct;
import tutorial.Calculator.Iface;
import tutorial.InvalidOperation;
import tutorial.Work;

@Service
@Slf4j
public class CalculatorImpl extends SharedImpl implements Iface {
    public CalculatorImpl() {
    }

    @Override
    public SharedStruct getStruct(int key) throws TException {
        SharedStruct result = new SharedStruct();
        result.setKey(key);
        result.setValue("Always the same (Calculator).");
        return result;
    }

    @Override
    public int add(int num1, int num2) throws TException {
        return num1 + num2;
    }

    @Override
    public int calculate(int logid, Work work) throws InvalidOperation, TException {
        int result;
        switch (work.getOp()) {
            case ADD:
                result = work.getNum1() + work.getNum2();
                break;
            case SUBTRACT:
                result = work.getNum1() - work.getNum2();
                break;
            case MULTIPLY:
                result = work.getNum1() * work.getNum2();
                break;
            case DIVIDE:
                result = work.getNum1() / work.getNum2();
                break;
            default:
                throw new IllegalArgumentException("Unknown operation: " + work.getOp());
        }
        return result;
    }

    @Override
    public void ping() throws TException {
        System.out.println("Server pinged.");
    }

    @Override
    public void zip() throws TException {
        System.out.println("Server zipped.");
    }
}